package com.kolon.business.mServiceDesk.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mServiceDesk.controller.MServiceDeskNotificationController")
public class MServiceDeskNotificationController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/business/mServiceDesk/mServiceDeskNotification.do")
	public ModelAndView controller(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션 아이디 및 암호 값을 불러옴
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		String tid = id;
		//권한을 새션에 호출
		HttpSession session = request.getSession(true);
		String assignee = (String)session.getAttribute("SESSION_ASSIGNEE");
		String approval = (String)session.getAttribute("SESSION_APPROVAL");
		String ref_num = request.getParameter("Ref_Num");
		String ref_type = request.getParameter("Ref_Type");
		//String returnUrl = "/business/mServiceDesk/mServiceDeskProcessView.do?Ref_Num="+ref_num +"&Ref_Type="+ref_type;
		String returnUrl = request.getContextPath() + "/business/mServiceDesk/mServiceDeskProcessList.do?Status=ALERT";
		ModelAndView modelAndView = new ModelAndView();
		List mapList = new ArrayList();
		//#######################################################################################
		// 처리계획 알림 처리
		//#######################################################################################
		if("Save".equals(ComUtil.nullChk(request.getParameter("MODE")))) {
			StringBuffer buffer2 = new StringBuffer();
			buffer2.append(url);
			buffer2.append("/servicedesk.aspx?mode=").append("action");
			buffer2.append("&actionMode=").append("4");
			buffer2.append("&ref_num=").append(ref_num);
			buffer2.append("&accept_desc=").append(ComUtil.getEncode(request.getParameter("accept_desc"),"EUC-KR"));
			buffer2.append("&atOnce_btn=").append(request.getParameter("atOnce_btn"));
			buffer2.append("&userid=").append(id);
			// XML DOCUMENT 및 인증 처리
			ComUtil.getDocument(buffer2.toString(), id, pw);
			modelAndView.addObject("message", "처리되었습니다.");
			modelAndView.addObject("scriptType", "003");
			modelAndView.addObject("message_url", returnUrl);
			modelAndView.setViewName("/business/common/alert");
		//#######################################################################################
		}else{
		//#######################################################################################
			StringBuffer buffer = new StringBuffer();
			buffer.append(url);
			Map map = new HashMap();
			//#######################################################################################
			// 상황별 메시지 리시트 처리
			//#######################################################################################
			buffer.append("/servicedesk.aspx?mode=").append("6");
			buffer.append("&ref_num=").append(ComUtil.nullChk(request.getParameter("Ref_Num")));
			buffer.append("&userid=").append(tid);
			// XML DOCUMENT 및 인증 처리
			Document doc = ComUtil.getDocument(buffer.toString(), id, pw);
			Element element = doc.getRootElement();
			List xmlList = element.getChildren("Table");
			Iterator itXml = xmlList.iterator();
			// XML 데이터 
			while (itXml.hasNext()) {
				Element el = (Element)itXml.next();
				map = new HashMap();
				map.put("num", el.getChild("num").getText());//처리계획
				map.put("desc", el.getChild("desc").getText().replaceAll("\n", "").replaceAll("\r", ""));          //내용 
				mapList.add(map);
			}	
			//#######################################################################################
		}
		// 리턴
		modelAndView.addObject("mapList", mapList);
		modelAndView.addObject("assignee", assignee);
		modelAndView.addObject("approval", approval);
		modelAndView.addObject("Ref_Num", ref_num);
		modelAndView.addObject("Ref_Type", ref_type);
		return modelAndView;
	}	
	
}